package com.engineering.elink.controller.file;

import java.io.IOException;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.engineering.elink.common.util.BaseCo;
import com.engineering.elink.controller.BaseController;
import com.engineering.elink.datasource.DataSourceContextHolder;
import com.engineering.elink.datasource.DataSourceType;
import com.engineering.elink.service.file.FileService;

@Controller
@RequestMapping("/system/file")
public class FileController extends BaseController {
	@Autowired
	private FileService fileService;

	private static final String FILE_LIST = getViewPath("admin/file/file_list");
	
	/** 创建或者修改文件 */
	private static final String ADD_USER = getViewPath("admin/file/add_file");
	
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("pageInfo")
    public void initBinderQueryPageInfo(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("pageInfo.");
    }
    
	/**
	 * 获取工程资料库列表
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequiresPermissions("file_list:view")
	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView mainIndex() throws IOException {
//		
		ModelAndView modelAndView = new ModelAndView(FILE_LIST);
//		
//		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
//		System.out.println("选择的数据源"+DataSourceContextHolder.getDbType());
//		
//		
//		//用户总数量
//		int userNumber = 100;
//		modelAndView.addObject("userNumber", userNumber);
//		String updateTime = DateUtil.formatDateTime(new Date());
//		modelAndView.addObject("updateTime", updateTime);
//		//资料列表
//		//JSON.toJSONString(fileService.selectList(offset,limit))
//		modelAndView.addObject("rows", fileService.selectList(offset,limit));
//		int count = fileService.selectCount();
//		modelAndView.addObject("totalCount", count);
		return modelAndView;
	}
	
	
	/**
	 * 获取列表
	 * @param co
	 * @return
	 */
	@RequiresPermissions("file_list:view")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getList(	@ModelAttribute @Valid BaseCo co) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据源"+DataSourceContextHolder.getDbType());
		
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("rows", fileService.selectList(offset, limit));
//		int count = fileService.selectCount();
//		map.put("totalCount", count);

		return  fileService.selectList(co);
	}	
	
	
	@RequiresPermissions("file_list:add")
	@RequestMapping(value = "/list/add", method = RequestMethod.GET)
	public String add(){
		return ADD_USER;
	}
}
